<template>
  <div class="app-container" :class="{ 'sidebar-expanded': sidebarExpanded }">
    <!-- 侧边栏 -->
    <Sidebar 
      class="sidebar-container" 
      :class="{ 'collapsed': !sidebarExpanded }"
      :collapsed="!sidebarExpanded"
    />
    
    <!-- 主内容区 -->
    <div class="main-container">
      <!-- 头部 -->
      <header class="header">
        <div class="menu-toggle mobile-only" @click="toggleMobileMenu">
          <el-icon><Menu /></el-icon>
        </div>
        <div class="collapse-toggle desktop-only" @click="toggleSidebar">
          <el-icon v-if="sidebarExpanded"><Fold /></el-icon>
          <el-icon v-else><Expand /></el-icon>
        </div>
        <h1 class="page-title" :style="{ position: 'relative', zIndex: 10 }">Go-DDNS 管理控制台</h1>
        <div class="header-actions">
          <el-button circle @click="refreshPage">
            <el-icon><Refresh /></el-icon>
          </el-button>
        </div>
      </header>
      
      <!-- 移动端菜单抽屉 -->
      <el-drawer
        v-model="mobileMenuVisible"
        direction="ltr"
        size="200px"
        :with-header="false"
        custom-class="mobile-drawer"
      >
        <div class="drawer-logo-container">
          <div class="logo-wrapper">
            <el-icon class="logo-icon"><Connection /></el-icon>
            <span class="logo-text">DDNS</span>
          </div>
        </div>
        <el-menu 
          router 
          :default-active="activeRoute"
          @select="handleSelect"
          background-color="var(--sidebar-bg)"
          text-color="var(--menu-text)"
          active-text-color="var(--primary-color)"
        >
          <el-menu-item index="/">
            <el-icon><HomeFilled /></el-icon>
            <span>首页</span>
          </el-menu-item>
          
          <el-menu-item index="/ddns-config">
            <el-icon><Setting /></el-icon>
            <span>DDNS配置</span>
          </el-menu-item>
          
          <el-menu-item index="/file-server">
            <el-icon><Folder /></el-icon>
            <span>文件服务器</span>
          </el-menu-item>
          
          <el-menu-item index="/about">
            <el-icon><InfoFilled /></el-icon>
            <span>关于</span>
          </el-menu-item>
        </el-menu>
      </el-drawer>
      
      <!-- 页面内容 -->
      <main class="content">
        <router-view />
      </main>
    </div>
  </div>
</template>

<script>
import { ref, computed, provide } from 'vue'
import { useRoute } from 'vue-router'
import Sidebar from '@/components/Sidebar.vue'
import { 
  Menu, 
  Refresh, 
  HomeFilled, 
  Setting, 
  Folder, 
  InfoFilled,
  Connection,
  Expand,
  Fold
} from '@element-plus/icons-vue'

export default {
  name: 'App',
  components: {
    Sidebar,
    Menu,
    Refresh,
    HomeFilled,
    Setting,
    Folder,
    InfoFilled,
    Connection,
    Expand,
    Fold
  },
  setup() {
    const route = useRoute()
    const mobileMenuVisible = ref(false)
    const sidebarExpanded = ref(true)
    
    const activeRoute = computed(() => {
      return route.path
    })
    
    const toggleMobileMenu = () => {
      mobileMenuVisible.value = !mobileMenuVisible.value
    }
    
    const toggleSidebar = () => {
      sidebarExpanded.value = !sidebarExpanded.value
      // 通知侧边栏组件
      setTimeout(() => {
        window.dispatchEvent(new Event('resize'))
      }, 100)
    }
    
    const handleSidebarCollapse = (collapsed) => {
      sidebarExpanded.value = !collapsed
      setTimeout(() => {
        window.dispatchEvent(new Event('resize'))
      }, 100)
    }
    
    const handleSelect = () => {
      mobileMenuVisible.value = false
    }
    
    const refreshPage = () => {
      window.location.reload()
    }
    
    // 提供侧边栏状态给子组件
    provide('sidebarExpanded', sidebarExpanded)
    
    return {
      mobileMenuVisible,
      sidebarExpanded,
      activeRoute,
      toggleMobileMenu,
      toggleSidebar,
      handleSidebarCollapse,
      handleSelect,
      refreshPage
    }
  }
}
</script>

<style>
@import './assets/styles/main.css';

/* 抽屉样式 */
.mobile-drawer .el-drawer__body {
  padding: 0;
  background-color: var(--sidebar-bg);
}

.drawer-logo-container {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  height: var(--header-height);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.drawer-logo-container .logo-wrapper {
  display: flex;
  align-items: center;
}

.drawer-logo-container .logo-icon {
  font-size: 28px;
  color: var(--primary-color);
}

.drawer-logo-container .logo-text {
  font-size: 24px;
  font-weight: bold;
  color: var(--primary-color);
  margin-left: 10px;
}
</style>

<style scoped>
.app-container {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

.sidebar-container {
  display: none;
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  z-index: 900;
  transition: width 0.15s;
  width: var(--sidebar-width);
}

.sidebar-container.collapsed {
  width: var(--sidebar-collapsed-width);
}

.main-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: margin-left 0.15s;
  width: 100%;
  margin-left: var(--sidebar-width);
}

.app-container.sidebar-expanded .main-container {
  margin-left: var(--sidebar-width);
}

.app-container:not(.sidebar-expanded) .main-container {
  margin-left: var(--sidebar-collapsed-width);
}

.header {
  display: flex;
  align-items: center;
  height: var(--header-height);
  padding: 0 16px;
  background-color: var(--card-bg-color);
  box-shadow: 0 1px 4px var(--shadow-color);
  z-index: 950;
  width: 100%;
  position: relative;
}

.menu-toggle, .collapse-toggle {
  font-size: 24px;
  margin-right: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
}

.mobile-only {
  display: flex;
}

.desktop-only {
  display: none;
}

.page-title {
  flex: 1;
  font-size: 20px;
  font-weight: 500;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-actions {
  display: flex;
  gap: 8px;
}

.content {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
  background-color: var(--bg-color);
}

/* 桌面版样式 */
@media (min-width: 768px) {
  .sidebar-container {
    display: block;
  }
  
  .mobile-only {
    display: none;
  }
  
  .desktop-only {
    display: flex;
  }
}
</style> 